#include "StdAfx.h"
#include "Character.h"
#include "PhysX.h"
#include "GraphCamera.h"
#include "NxController.h"
#include "PhysX.h"
#include "PickableItem.h"

Character::Character(GraphCamera* pGraphCamera,D3DXVECTOR3 start,PhysX* pPhysX):
	m_pCamera(pGraphCamera),
	m_PosW(start),
	m_pPhysX(pPhysX),
	m_RunningMultiplicationFactor(2.5f),m_Speed(5.0f),m_MouseScale(100),m_Radius(.4f),m_Height(1)
	,m_PreviousPosW(start)
{
	m_pController = m_pPhysX->CreateController(this);
}

Character::~Character(void)
{
	if ( m_pCamera !=  0 )
		delete m_pCamera;
	
	vector<PickableItem*>::iterator it;
	for ( it = m_Inventory.begin(); it != m_Inventory.end(); ++it){
		delete *it;
	}
}

tstring Character::GetContactShape(){

	return _T("No shape");
}

bool Character::HasContact()
{
	return false;
}

void Character::Tick(const InputState& state){

	bool bRunning=false;

	// Find the net direction the camera is traveling in (since the
	// camera could be running and strafing).
	D3DXVECTOR3 dir(0.0f, 0.0f, 0.0f);
	if(state.IsKeyDown('Z') )
		dir += m_pCamera->GetLook();
	if(state.IsKeyDown('S') )
		dir -= m_pCamera->GetLook();
	if(state.IsKeyDown('D') )
		dir += m_pCamera->GetRight();
	if(state.IsKeyDown('Q') )
		dir -= m_pCamera->GetRight();
	if(state.IsKeyDown(VK_LSHIFT) )
		bRunning = true;

	//Misschien een config maken voor de controles van de speler
	if(state.IsKeyDown(VK_ESCAPE) )
		exit(0);
	
	// Move at mSpeed along net direction.
	D3DXVec3Normalize(&dir, &dir);
	float finalspeed = m_Speed;

	if(bRunning)finalspeed*=m_RunningMultiplicationFactor;
	//use dt so that speed is independent from framerate 
	dir *= finalspeed*state.GetDeltaTime();
	/*if ( dir.y > 0 )
		dir.y = 0;*/

	NxU32 flags;

	if ( state.GetDeltaTime() > 0.001f)
	{
		if ( !HasContact() ){
			// add gravity code.
			//D3DXVECTOR3 diff = m_PosW - m_PreviousPosW;

			//float vy = diff.y / state.GetDeltaTime();

			//vy -= (4.9f * state.GetDeltaTime()*state.GetDeltaTime());
			//dir.y += vy * state.GetDeltaTime();

		}
	}
	NxVec3 disp(dir.x, dir.y, dir.z);
		flags = NXCC_COLLISION_SIDES;

		m_pController->move(disp, 1, 0, flags);
		m_PreviousPosW = m_PosW;
		NxVec3 NewPos;
		NewPos = m_pController->getActor()->getGlobalPosition();
		m_PosW.x = NewPos.x;
		m_PosW.y = NewPos.y;
		m_PosW.z = NewPos.z;

		//Mouse movement stuff
		RECT window;
		window = state.GetWindowRect();

		int MouseCenterXpos = (int)(window.left + (window.right - window.left)*0.5);
		int MouseCenterYpos = (int)(window.top + (window.bottom - window.top)*0.5);


		float MousePitch = state.GetMouseMovement().y / m_MouseScale;
		float MouseRotate = state.GetMouseMovement().x / m_MouseScale;

		if(state.IsLeftMouseButtonDown())
		{
			m_pCamera->Move(m_PosW, true, MousePitch, MouseRotate);
		} else {
			m_pCamera->Move(m_PosW);
		}

		//SetCursorPos(MouseCenterXpos, MouseCenterYpos);
}

void Character::SetMouseScale(int scale)
{
	if(scale>1 && scale<1000)
		m_MouseScale = (float)scale;
}

bool Character::HasItem(int id){
	return false;
}
